package com.instalinks.android.apps.smspostservice;

import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.instalinks.android.apps.smspostservice.helper.ServiceStartStatus;

public class HomeActivity extends AppTabBaseActivity {

	private TextView txtUserType;
	private TextView txtValidityStatus;

	private TextView txtSmsSuccess;
	private TextView txtSmsInvalid;
	private TextView txtSmsDropped;
	private TextView txtSmsFailed;
	private TextView txtWebSms;

	private ToggleButton btnServiceOnOff;
	private Handler mHandler;

	protected void doInitialize() {
		mAppCfg.rePopulate();

		setContentView(R.layout.home);

		// SMS COUNT
		this.txtSmsSuccess = (TextView) findViewById(R.id.hm_txtSmsSuccess);
		this.txtSmsDropped = (TextView) findViewById(R.id.hm_txtSmsDropped);
		this.txtSmsInvalid = (TextView) findViewById(R.id.hm_txtSmsInvalid);
		this.txtSmsFailed = (TextView) findViewById(R.id.hm_txtSmsFailed);
		this.txtWebSms = (TextView) findViewById(R.id.hm_txtWebSms);

		// SUBSCRIPTION
		this.txtUserType = (TextView) findViewById(R.id.hm_txtUserType);
		this.txtValidityStatus = (TextView) findViewById(R.id.hm_txtValidity);

		btnServiceOnOff = (ToggleButton) this
				.findViewById(R.id.btnServiceOnOff);

		btnServiceOnOff.setChecked(mAppCfg.isSmsServiceRunning());

		btnServiceOnOff.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (btnServiceOnOff.isChecked()) {
					startServiceThread();
				} else {
					stopServiceThread();
				}
			}
		});

	}

	protected void doPostInitialize() {

		try {
			mHandler = new Handler();
			mHandler.removeCallbacks(statusUpdater);
			mHandler.post(statusUpdater);
		} catch (Exception e) {
			Log.e("[SMSPOSTSERVICE]", e.getMessage());
		}
		
		setMode();
	}

	private void setValues() {
		mAppCfg.rePopulate();

		this.txtSmsSuccess
				.setText(String.valueOf(mAppCfg.getSmsSuccessCount()));
		this.txtSmsDropped
				.setText(String.valueOf(mAppCfg.getSmsDroppedCount()));
		this.txtSmsInvalid
				.setText(String.valueOf(mAppCfg.getSmsInvalidCount()));
		this.txtSmsFailed.setText(String.valueOf(mAppCfg.getSmsFailedCount()));

		this.txtWebSms.setText(String.valueOf(mAppCfg.getWebSmsCount()));

		this.txtUserType.setText((mAppCfg.isPaidFlag() ? "Paid" : "Demo"));
		this.txtValidityStatus.setText(mAppCfg.getValidityStatus());
	}

	private Runnable statusUpdater = new Runnable() {
		@Override
		public void run() {
			setValues();
			mHandler.postDelayed(this, com.instalinks.android.lib.enums.TimeOuts.TwoSec.getValue());
		}
	};

	private void setMode() {
		boolean isServiceInProgress = mAppCfg.isSmsServiceRunning();
		setMode(isServiceInProgress);
	}

	private void setMode(boolean isServiceInProgress) {
		btnServiceOnOff.setChecked(isServiceInProgress);
		btnServiceOnOff
				.setBackgroundResource(btnServiceOnOff.isChecked() ? R.drawable.toggle_on
						: R.drawable.toggle_off);
	}

	private void startServiceThread() {
		try {
			mAppCfg.setAvailableFreeCount(mAppCfg.getFreeCount());
		} catch (Exception e) {
		}

		new SubTask("Starting Service").execute();
	}

	private boolean stopServiceThread() {
		try {
			mAppCfg.setSmsServiceStatus(false);
			setMode(false);
		} catch (Exception e) {
			mAppCfg.alert("Error stopping SMS Service");
			return false;
		}

		return true;
	}

	private ServiceStartStatus startService() {
		mAppCfg.rePopulate();

		ServiceStartStatus status = mAppCfg.allGoodToRunService();

		if (status == ServiceStartStatus.Ok) {
			boolean result = reloadConfigFromHost();
			if (result) {
				try {
					mAppCfg.setSmsServiceStatus(true);
					return ServiceStartStatus.Started;
				} catch (Exception e) {
					return ServiceStartStatus.UnknownIssueStartingSmsService;
				}
			} else {
				return ServiceStartStatus.IssueUpdatingConfigFile;
			}
		} else {
			return status;
		}
	}

	@Override
	protected boolean doBackgroundWork() {
		ServiceStartStatus status = startService();
		if (status == ServiceStartStatus.Ok
				|| status == ServiceStartStatus.Started) {
			return true;
		} else {
			mAppCfg.handleServiceResponse(status);
			return false;
		}
	}

	@Override
	protected void doOnSuccess() {
		setMode(true);
	}

	@Override
	protected void doOnFailure() {
		setMode(false);
	}
}
